//
//  CustomTitleView.m
//  U9WatchGame
//
//  Created by uuu9 on 16/3/14.
//  Copyright © 2016年 Jason. All rights reserved.
//

#import "CustomTitleView.h"
#import "CustomLabel.h"


@implementation CustomTitleView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(0xffffff);
    }
    return self;
}


- (void)setThemesArray:(NSMutableArray *)themesArray
{
    _themesArray = themesArray;
    for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        
    }
    for (NSInteger i = 0; i < self.themesArray.count; i++) {
        CustomLabel *label = [[CustomLabel alloc] initWithFrame:CGRectMake(i * self.tagWidth, 0, self.tagWidth, self.tagHeight)];
        label.text = themesArray[i];
        CGFloat width = [label.text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.tagHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.width;
        if (width > self.tagWidth) {
            label.font = [UIFont systemFontOfSize:13];
        }
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTheme:)]];
        label.tag = i;
        [self addSubview:label];
        
        if (i == 0) { // 最前面的label
            label.scale = 1.0;
        }
    };
    CGFloat contentWidth = themesArray.count *self.tagWidth;
    if (contentWidth < CGRectGetWidth(self.frame)) {
        contentWidth = CGRectGetWidth(self.frame);
    }
    self.contentSize = CGSizeMake(contentWidth  , 36);
}



- (void)clickTheme:(UITapGestureRecognizer *)sender
{
   NSInteger index = sender.view.tag;
    _ClickThemeBlock(index);
    
    
}


@end
